import Vue from 'vue'
import Layout from '@/views/layout/Layout'
const _import = require('./_import_' + process.env.NODE_ENV)
export function filterRoutes(routers) {
  const routerTree = []
  for (let i = 0; i < routers.length; i++) {
    const obj = routers[i]
    const newObj = {
      path: obj.path,
      name: obj.name,
      meta: {
        title: obj.name,
        icon: obj.icon,
        level: obj.menuType,
        breadcrumb:
          (obj.children.length > 0 && obj.menuType == 1) || obj.menuType == 3
            ? false
            : true,
        identifying: obj.identifying,
      },
      hidden: false,
    }
    if (obj.menuType == 1) {
      Vue.set(newObj, 'component', Layout)
      if (obj.path == '/home') {
        Vue.set(newObj, 'redirect', 'home')
        Vue.set(newObj, 'path', '')
      }
    } else if (obj.menuType == 2) {
      Vue.set(newObj, 'component', _import(obj.pid_path + obj.path))
      if (obj.children.length > 0) {
        obj.children.forEach((item) => {
          newObj.meta.identifying += item.identifying + ','
        })
      }
      //设计师资料
      if (
        obj.path == '/profile' ||
        obj.path == '/sendOrder' ||
        obj.path == '/auditOrder'
      ) {
        newObj.hidden = true
      }
      //首页
      if (
        obj.pid_path == '/home' ||
        obj.pid_path == '/financialManage' ||
        obj.pid_path == '/templateManage' ||
        obj.pid_path == '/articleManage'
      ) {
        newObj.path = obj.pid_path
      }
    }
    //递归
    if (obj.children && obj.menuType != 2) {
      Vue.set(newObj, 'children', filterRoutes(obj.children))
    }
    routerTree.push(newObj)
  }
  return routerTree
}
